<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryRef" :inline="true" :model="queryParams" label-width="68px">
      <el-form-item label="物品编码" prop="goodsCode">
        <el-input
            v-model="queryParams.goodsCode"
            clearable
            placeholder="请输入物品编码"
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="批次号" prop="batchCode">
        <el-input
            v-model="queryParams.batchCode"
            clearable
            placeholder="请输入批次号"
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="单据号" prop="orderId">
        <el-input
            v-model="queryParams.orderId"
            clearable
            placeholder="请输入单据号"
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="仓库ID" prop="warehouseId">
        <el-input
            v-model="queryParams.warehouseId"
            clearable
            placeholder="请输入仓库ID"
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="库位ID" prop="areaId">
        <el-input
            v-model="queryParams.areaId"
            clearable
            placeholder="请输入库位ID"
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button icon="Search" type="primary" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            v-hasPermi="['wms:stockLog:export']"
            icon="Download"
            plain
            type="warning"
            @click="handleExport"
        >导出
        </el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="stockLogList" @selection-change="handleSelectionChange">
      <el-table-column align="center" type="selection" width="55"/>
      <el-table-column align="center" label="事务ID" prop="id"/>
      <el-table-column align="center" label="物品编码" prop="goodsCode"/>
      <el-table-column align="center" label="单位" prop="measureUnit"/>
      <el-table-column align="center" label="批次号" prop="batchCode"/>
      <el-table-column align="center" label="单据号" prop="orderId"/>
      <el-table-column align="center" label="仓库ID" prop="warehouseId"/>
      <el-table-column align="center" label="库位ID" prop="areaId"/>
      <el-table-column align="center" label="订单操作前" prop="quantityBefore"/>
      <el-table-column align="center" label="订单操作后" prop="quantityAfter"/>
      <el-table-column align="center" label="操作数量" prop="quantity"/>
      <el-table-column align="center" label="备注" prop="remark"/>
      <el-table-column align="center" label="创建者" prop="createBy"/>
      <el-table-column align="center" label="创建时间" prop="createTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="更新者" prop="updateBy"/>
      <el-table-column align="center" label="更新时间" prop="updateTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        v-model:limit="queryParams.pageSize"
        v-model:page="queryParams.pageNum"
        :total="total"
        @pagination="getList"
    />
  </div>
</template>

<script name="StockLog" setup>
import {listStockLog} from "@/api/wms/stock";

const {proxy} = getCurrentInstance();

const stockLogList = ref([]);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    goodsCode: null,
    batchCode: null,
    orderId: null,
    warehouseId: null,
    areaId: null,
  },
  rules: {}
});

const {queryParams, form, rules} = toRefs(data);

/** 查询出入库记录列表 */
function getList() {
  loading.value = true;
  listStockLog(queryParams.value).then(response => {
    stockLogList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('wms/stockLog/export', {
    ...queryParams.value
  }, `stockLog_${new Date().getTime()}.xlsx`)
}

getList();
</script>
